//
//  CPSCHomeWaterfallViewController.m
//  CollTest
//
//  Created by codew on 9/10/21.
//

#import "CPSCHomeWaterfallViewController.h"
#import "CPSCWaterfallLayout.h"
#import "CPSCServiceListCollectionViewCell.h"

@interface CPSCHomeWaterfallViewController ()<UICollectionViewDataSource, CPSCWaterfallLayoutDelegate>

@property (nonatomic, strong) UICollectionView *collectionView;

@end

static NSString * CPSCServiceListCollectionViewCellIdentifier = @"CPSCServiceListCollectionViewCell";

@implementation CPSCHomeWaterfallViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    
    [self setupCollectionViewLayout];
}

- (void)setupCollectionViewLayout
{
    // 创建布局
    CPSCWaterfallLayout *layout = [[CPSCWaterfallLayout alloc] init];
    layout.delegate = self;
    
    // 创建CollectionView
    UICollectionView *collectionView = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:layout];
    collectionView.backgroundColor = [UIColor colorWithHexString:@"#F5F5F5"];
    collectionView.dataSource = self;
    [self.view addSubview:collectionView];
    // 注册
    [collectionView registerClass:CPSCServiceListCollectionViewCell.class forCellWithReuseIdentifier:CPSCServiceListCollectionViewCellIdentifier];
    self.collectionView = collectionView;
}


#pragma mark - UICollectionViewDelegate

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    
}

#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    
    return 30;
}


- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    CPSCServiceListCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:CPSCServiceListCollectionViewCellIdentifier forIndexPath:indexPath];

    cell.backgroundColor = kCPSCRandomColor;
    [cell updateCellWithItem:nil];
    return cell;
}

#pragma mark - CPSCWaterfallLayoutDelegate
/** 每一个item的高度 */
- (CGFloat)waterflowLayout:(CPSCWaterfallLayout *)waterflowLayout
      heightForItemAtIndex:(NSUInteger)index
                 itemWidth:(CGFloat)itemWidth
{
    return 181 + arc4random_uniform(30);;
}

/** 列数 */
- (CGFloat)columnCountInWaterflowLayout:(CPSCWaterfallLayout *)waterflowLayout
{
    return 2;
}

/** 每一列之间的间隙 */
- (CGFloat)columnMarginInWaterflowLayout:(CPSCWaterfallLayout *)waterflowLayout
{
    return 10;
}

/** 每一行之间的间隙 */
- (CGFloat)rowMarginInWaterflowLayout:(CPSCWaterfallLayout *)waterflowLayout
{
    return 10;
}

/** 每一个item 上下左右 的间隙 */
- (UIEdgeInsets)edgeInsetsInWaterflowLayout:(CPSCWaterfallLayout *)waterflowLayout
{
    return UIEdgeInsetsMake(10, 10, 10, 10);
}

@end
